import javax.swing.*;
import java.beans.*;
import java.util.Observer;
import java.util.Observable;
import java.sql.*; 
import java.lang.*;
public class BuscarMaterial extends Observable implements Observer{
 
    
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
   	JFrame ventanaBuscarMaterial;
 	
 	M_Material material;
 	ResultadosBusquedaMaterial ventanaResultados;
 	
 	int i;
    int j;
    boolean editando;
 	String seleccionado;
 
 	public BuscarMaterial(){
	 	material=new M_Material();
	 	levantaVista();
 	}
 

    private void levantaVista() {
        ventanaBuscarMaterial=new JFrame();
      	jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jComboBox2 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();

        ventanaBuscarMaterial.setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        ventanaBuscarMaterial.setTitle("Sistema de administracion... - Buscar material y volumenes");
        ventanaBuscarMaterial.setResizable(false);
        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel1.setText("Busqueda de materiales y volumenes");

        jLabel2.setText("Ingrese los criterios de busqueda:");

        jLabel3.setText("Ingrese nombre o titulo:");

        jLabel4.setText("Ingrese autor:");

        jLabel5.setText("Ingrese el tipo de material:");
		llenaComboBox();
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel7.setText("Ingrese tematica:");

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Volver");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel8.setText("Ingrese titulo:");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(ventanaBuscarMaterial.getContentPane());
        ventanaBuscarMaterial.getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                    .add(jLabel2)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel8)
                            .add(jLabel5)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jLabel7)
                            .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(132, 132, 132)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jComboBox2, 0, 0, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 93, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jTextField3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jTextField1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 189, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, jComboBox1, 0, 189, Short.MAX_VALUE))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel1)
                .add(21, 21, 21)
                .add(jLabel2)
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel8)
                    .add(jTextField2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(jTextField3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(26, 26, 26)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel7)
                    .add(jComboBox2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 41, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton2)
                    .add(jButton1))
                .addContainerGap())
        );
        ventanaBuscarMaterial.pack();
        ventanaBuscarMaterial.setVisible(true);
        jButton1.grabFocus();
    }

   
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {
	
	}
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
	
		if(jComboBox1.getSelectedIndex()==i-1 && jComboBox2.getSelectedIndex()==j-1){
			System.out.println("ambos cualquiera");
			material.buscar(jTextField1.getText(),jTextField2.getText(),jTextField3.getText());
			ventanaResultados = new ResultadosBusquedaMaterial(material.resultadoBusqueda, material.numeroResultados);
			//material=new M_Material();
			material.reset();
			ventanaResultados.addObserver(this);
			ventanaBuscarMaterial.setVisible(false);
		}
		
		if(jComboBox1.getSelectedIndex()!=i-1 && jComboBox2.getSelectedIndex()!=j-1){
			System.out.println("ninguno cualquiera");
			material.buscar(material.idTiposMateriales[jComboBox1.getSelectedIndex()],jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),material.idTematicasMateriales[jComboBox2.getSelectedIndex()]);
			ventanaResultados = new ResultadosBusquedaMaterial(material.resultadoBusqueda, material.numeroResultados);
			//material=new M_Material();
			material.reset();
			ventanaResultados.addObserver(this);
			ventanaBuscarMaterial.setVisible(false);
		}
		
		if(jComboBox1.getSelectedIndex()!=i-1 && jComboBox2.getSelectedIndex()==j-1){
			System.out.println("segundo cualquiera");
			material.buscar(material.idTiposMateriales[jComboBox1.getSelectedIndex()],jTextField1.getText(),jTextField2.getText(),jTextField3.getText());
			ventanaResultados = new ResultadosBusquedaMaterial(material.resultadoBusqueda, material.numeroResultados);
			//material=new M_Material();
			material.reset();
			ventanaResultados.addObserver(this);
			ventanaBuscarMaterial.setVisible(false);
		}
		
		if(jComboBox1.getSelectedIndex()==i-1 && jComboBox2.getSelectedIndex()!=j-1){
			System.out.println("primero cualquiera"+material.idTematicasMateriales[jComboBox2.getSelectedIndex()]);
			material.buscar(jTextField1.getText(),jTextField2.getText(),jTextField3.getText(),material.idTematicasMateriales[jComboBox2.getSelectedIndex()]);
			ventanaResultados = new ResultadosBusquedaMaterial(material.resultadoBusqueda, material.numeroResultados);
			//material=new M_Material();
			material.reset();
			ventanaResultados.addObserver(this);
			ventanaBuscarMaterial.setVisible(false);
		}
	}

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
		this.setChanged();
		this.notifyObservers(); 
		ventanaBuscarMaterial.dispose();

    }
    
  
    public static void main(String args[]) {
  	BuscarMaterial e=new BuscarMaterial();
    }
    
	public void compruebaTipoSeleccionado(){
	
		if(material.tiposMaterialesInfo[jComboBox1.getSelectedIndex()] ==0){
			jTextField3.setEnabled(false);
			jComboBox2.setEnabled(false);
			seleccionado = "libro";
		}
		if(material.tiposMaterialesInfo[jComboBox1.getSelectedIndex()] ==1){
			jTextField3.setEnabled(true);
			jComboBox2.setEnabled(true);
			seleccionado="otro";
		}
	}	  

   	private void llenaComboBox(){
		try{
			material.getTiposMateriales();
			material.getCategorias();
		
			for(i=0;i<material.numeroTiposMateriales;i++){
		    	jComboBox1.insertItemAt(material.tiposMateriales[i],i);
		    	System.out.println(material.tiposMateriales[i]);
		    	
		    }
		    jComboBox1.insertItemAt("Cualquiera",i++);
		    
		    for(j=0;j<material.numeroTematicasMateriales;j++){
		    	jComboBox2.insertItemAt(material.tematicasMateriales[j],j);
		    	System.out.println(material.tematicasMateriales[j]);
		    }
		    jComboBox2.insertItemAt("Cualquiera",j++);
	    }catch(Exception e){}
	    
	    jComboBox1.setSelectedIndex(i-1);
		jComboBox2.setSelectedIndex(j-1);
	}
 
	public void update( Observable obse,Object obje ) {
    
    	ventanaBuscarMaterial.setVisible(true);
   
    }
    
    
     
}